package com.amt.util;

import com.google.common.base.Strings;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**   
 * @Title: StringUtil.java 
 * @Package com.amt.util 
 * @Description: TODO(字符串处理工具类) 
 * @author 江伟  
 * @date 2019年4月15日 上午10:50:41 
 * Copyright (c) ©1994-2021 Scjydz.com All Rights Reserved.
 */
public class StringUtil {
	
	/**
	 * @description: 半角转全角
	 * @param input 半角值
	 * @return 全角值
	 */
	public static String ToSBC(String input) {
		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == ' ') {
				c[i] = '\u3000';
			} else if (c[i] < '\177') {
				c[i] = (char) (c[i] + 65248);

			}
		}
		return new String(c);
	}

	/**
	 * @description: 全角转半角
	 * @param input 全角值
	 * @return 半角值
	 */
	public static String ToDBC(String input) {

		char c[] = input.toCharArray();
		for (int i = 0; i < c.length; i++) {
			if (c[i] == '\u3000') {
				c[i] = ' ';
			} else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
				c[i] = (char) (c[i] - 65248);

			}
		}
		String returnString = new String(c);

		return returnString;
	}
	
	/**
	 * @description: 字符串空判断
	 * @param str 字符串
	 * @return 空 返回true   不空返回false
	 */
	public static boolean isEmpty(String str) {
        if (str == null || str.length() == 0) {
            return true;
        }
        return false;
    }

	/**
	 * @description: 字符串非空判断
	 * @param str 字符串
	 * @return 不空 返回true   空返回false
	 */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }
    
    /**
	 * @description: 集合非空判断
	 * @param collection 集合
	 * @return 不空 返回true   空返回false
	 */
    public static boolean isNotEmpty(Collection<?> collection) {
   	 if (null != collection && collection.size() > 0) {
            return true;
        }
        return false;
   }
    
	/**
	 * @description: 集合非空判断
	 * @param collection 集合
	 * @return 不空 返回true   空返回false
	 */
    public static boolean isEmpty(Collection<?> collection) {
   	 if (null != collection && collection.size() > 0) {
            return false;
        }
        return true;
   }
    
    /**
	 * @description: 集合空判断
	 * @param list 集合
	 * @return 空 返回true   不空返回false
	 */
	public static boolean isEmpty(List<?> list) {
        if (null == list) {
            return true;
        }
        return false;
    }

	/**
	 * @description: 集合非空判断
	 * @param list 集合
	 * @return 不空 返回true   空返回false
	 */
    public static boolean isNotEmpty(List<?> list) {
    	 if (null != list && list.size() > 0) {
             return true;
         }
         return false;
    }
    
    /**
     * 
     * @description: 手机号排空+正则验证
     * @param str    手机号码
     * @return       true正确false错误
     */
    public static boolean isMobile(String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        String s2="^[1](([3|5|7|8|9][\\d])|([4][4,5,6,7,8,9])|([6][2,5,6,7])|([7][^9])|([9][1,8,9]))[\\d]{8}$";// 验证手机号
        if(StringUtil.isNotEmpty(str)){
            p = Pattern.compile(s2);
            m = p.matcher(str);
            b = m.matches();
        }
        return b;
    }
    
    /**
     * @description: 去除重复字符
     * @param str
     * @return
     */
    public static String removeSameString(String str) {
		String result = "";
		String[] commercialAccounts = str.split(",");
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < commercialAccounts.length; i++) {
			if (!list.contains(commercialAccounts[i])) {
				// 去除重复的字符串
				list.add(commercialAccounts[i]);
			}
		}
		String[] newStr = list.toArray(new String[0]); // 返回一个数组
		int i = 0;
		for (String tmp : newStr) {
			if (i == newStr.length - 1) {// 拼接时，不包括最后一个,字符
				result = result + tmp;
			} else {
				result = result + tmp + ",";
			}
			i++;
		}
		return result;

	}

	/**
	 * @Description: 获取6-10 的随机位数数字
	 * @param length	想要生成的长度
	 * @return result
	 */
	public static String getRandomNumber(Integer length) {
		String result = "";
		Random rand = new Random();
		int n = 20;
		if (null != length && length > 0) {
			n = length;
		}
		int randInt = 0;
		for (int i = 0; i < n; i++) {
			randInt = rand.nextInt(10);
			result += randInt;
		}
		return result;
	}


	/**
	 * @Description: 获取一个随机字符串
	 * @param length 字符串长度
	 * @return String    返回类型
	 */
	public static String getRandomString(int length) {
		String base = "abcdefghijklmnopqrstuvwxyz0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	/**
	 * @description: 获取12位随机数字
	 * @return 随机数
	 */
	public static String getRandomNumber() {
		Integer orderId = UUID.randomUUID().toString().hashCode();
		orderId = orderId < 0 ? - orderId : orderId;
		return 1+String.format("%011d", orderId);
	}

	// 保护用户隐私
	public static String replaceNameX(String str){
		// 获取姓名长度
		String custName = str;
		int length = custName.length();

		String reg = ".{1}";
		StringBuffer sb = new StringBuffer();
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(str);
		int i = 0;
		while(m.find()){
			i++;
			if(i==length)
				continue;
			m.appendReplacement(sb, "*");
		}
		m.appendTail(sb);
		return sb.toString();
	}

	public static String replaceIdNumber(String idNumber){
		if (!Strings.isNullOrEmpty(idNumber)) {
			if (idNumber.length() == 15){
				idNumber = idNumber.replaceAll("(\\w{1})\\w*(\\w{1})", "$1*************$2");
			}
			if (idNumber.length() == 18){
				idNumber = idNumber.replaceAll("(\\w{1})\\w*(\\w{1})", "$1****************$2");
			}
		}
		return idNumber;
	}

	public static String replacePhone(String phone){

		return StringUtil.isEmpty(phone) ? "" : phone.replaceAll("(\\w{3})\\w*(\\w{2})", "$1******$2");
	}

	/**
	 * 获取kettle ktr文件的类型标识
	 * @param param ktr文件名
	 * @return
	 */
	public static int getKettleFlag(String param) {
		if (param.indexOf("view_") != -1) {
			return 2;
		}else if (param.indexOf("interface_") != -1) {
			return 1;
		}
		return 0;
	}


}
